package com.example.practice.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.practice.R;
import com.example.practice.activity.VideoListActivity;
import com.example.practice.bean.CourseBean;

import java.util.List;

public class CourseAdapter extends BaseAdapter {
    private Context mContext;
    private List<CourseBean> data;

    private int [] courseImage={R.drawable.chapter_1_icon,
            R.drawable.chapter_2_icon,
            R.drawable.chapter_3_icon,
            R.drawable.chapter_4_icon,
            R.drawable.chapter_5_icon,
            R.drawable.chapter_6_icon,
            R.drawable.chapter_7_icon,
            R.drawable.chapter_8_icon,
            R.drawable.chapter_9_icon,
            R.drawable.chapter_10_icon};

    //    构造函数
    public CourseAdapter(Context mContext, List<CourseBean> data) {
        this.mContext = mContext;
        this.data = data;
    }

    class ViewHolder{
        public TextView tvImgTitle, tvTitle;
        public ImageView ivImage;
        public LinearLayout courseArea;
    }


    @Override
    public int getCount() {
//        Log.i("data", data+"");
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    //    设置页面
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder vh;
        if (convertView==null){
            vh=new ViewHolder();
            convertView= LayoutInflater.from(mContext).inflate(R.layout.course_list_item,parent,false);
            vh.ivImage=convertView.findViewById(R.id.iv_img);
            vh.tvImgTitle=convertView.findViewById(R.id.tv_img_title);
            vh.tvTitle=convertView.findViewById(R.id.tv_title);
            vh.courseArea=convertView.findViewById(R.id.ll_course_area);
            convertView.setTag(vh);
        } else {
            vh= (ViewHolder) convertView.getTag();
        }

        vh.ivImage.setImageResource(courseImage[position]);
        vh.tvImgTitle.setText(data.get(position).getImgTitle());
        vh.tvTitle.setText(data.get(position).getTitle());


        vh.courseArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, VideoListActivity.class);
                intent.putExtra("id",data.get(position).getId());
                intent.putExtra("intro",data.get(position).getIntro());
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }
}
